package com.yz.xyt.hadler;

import com.yz.xyt.constant.HttpStatus;
import com.yz.xyt.domain.ResponseResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理
 * 可根据异常类型进行扩展
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult handlerException(Exception e){
        ResponseResult result = new ResponseResult();
        result.setCode(HttpStatus.ERROR);
        if (e instanceof NullPointerException){
            result.setMsg("系统内部异常");
        }else{
            result.setMsg(e.getMessage());
        }

        return result;
    }
}
